import re
import jieba
from lxml import etree
from collections import Counter

"""
从本地打开页面，利用etree，xpath，re解析并提取文章的内容，并赋值给data，
然后利用re匹配英文单词和标点符号，利用jieba库对中文进行提取，
"""

file_path = r"C:\Users\Administrator\Desktop\1.html"
with open(file_path, 'r', encoding='gbk') as f:
    content = f.read()
    # print(content)

root = etree.HTML(content)
result = "".join(root.xpath("//div[@id='dede_content']//div/text()"))
# print(result)
lines = result.split('\n')
# 过滤空白行，保留非空行
non_empty_lines = [line for line in lines if line.strip()]
# 用换行拼接有效行，得到无空白行的内容
data = '\n'.join(non_empty_lines)
# 输出处理后的结果
print(data)
words = re.findall(r"[a-zA-Z']+", data)

# 提取数字
numbers = re.findall(r"\d+", data)

# 提取中文词汇（单字和词语）
chinese_chars = re.findall(r"[\u4e00-\u9fff]", data)

# 合并所有可能的候选，并转换为小写（英文）以便比较
candidates = [word.lower() for word in words] + numbers + chinese_chars

# 定义回文检查函数
def is_palindrome(s):
    # 对于包含撇号的英文单词，先去除撇号再检查
    if "'" in s:
        cleaned = s.replace("'", "")
        return cleaned == cleaned[::-1]
    return s == s[::-1]


# 筛选出所有回文
palindromes = [s for s in candidates if is_palindrome(s) and len(s) > 1]

# 找到最长的回文
if palindromes:
    longest_palindrome = max(palindromes, key=len)
    print(f"最长回文: {longest_palindrome}")
else:
    print("no tenet")

# # 英文词汇
# english_pattern = re.compile(r"[a-zA-Z']+")
# english_words = [word.lower() for word in english_pattern.findall(data)]
# english_counter = Counter(english_words)
#
# # 中文词汇统计
# chinese_pattern = re.compile(r"[\u4e00-\u9fff]+")
# chinese_text = "".join(chinese_pattern.findall(data))
# chinese_words = jieba.lcut(chinese_text, cut_all=False)
# chinese_counter = Counter(chinese_words)
#
# # 标点符号统计
# punctuation_pattern = re.compile(r"[^\w\u4e00-\u9fff\s]")
# punctuations = punctuation_pattern.findall(data)
# punctuation_counter = Counter(punctuations)
#
# # 输出结果
# print("英文单词统计")
# for word, count in english_counter.most_common():
#     print(f"{word}：{count}")
#
# print("中文词汇统计")
# for word, count in chinese_counter.most_common():
#     print(f"{word}：{count}")
#
# print("标点符号统计")
# for punctuation, count in punctuation_counter.most_common():
#     print(f"{punctuation}：{count}")
